import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
// import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:image_picker/image_picker.dart';

class ImagePickPage extends StatefulWidget {

  const ImagePickPage({ super.key });

  @override
  State createState() => _PickImagePageState();
}

class _PickImagePageState extends State<ImagePickPage> {
  final ImagePicker _picker = ImagePicker();

  Future<void> _pickImage(ImageSource source) async {
    final XFile? image = await _picker.pickImage(source: source);
    if (image != null) {
      // 处理选中的图片，例如显示图片或上传
      _saveImageToGallery(await image.readAsBytes());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('图片选择器'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextButton(
              child: const Text('从相册选择图片'),
              onPressed: () {
                _pickImage(ImageSource.gallery);
              },
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _saveImageToGallery(Uint8List imageData) async {
    // 假设你已经有一个Uint8List类型的图片数据imageData

    // 使用图像选择器保存图片
    // final result = await ImageGallerySaver.saveImage(imageData.buffer.asUint8List());
    // print(result);
    //
    // // 检查保存结果
    // if (result) {
    //   print('Image saved to gallery successfully');
    // } else {
    //   print('Failed to save image to gallery');
    // }
  }
}